package com.sgb.kjwl.view.ui.fragment.home;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.auction.entity.AuctionListEntity;
import com.sgb.auction.adapter.AuctionHallAdapter;
import com.sgb.auction.entity.PublishListBean;
import com.sgb.kjwl.model.entity.pick.TenderAllListNewEntity;
import com.sgb.kjwl.view.ui.adapter.pick.PickHallAllNewAdapter;
import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.BaseHeightViewPagerFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentBiddingSignUpBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.BannerEntity;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.sgb.kjwl.model.entity.pick.TenderAllListEntity;
import com.sgb.kjwl.view.ui.activity.pick.TenderDetailsActivity3;
import com.sgb.kjwl.view.ui.adapter.pick.PickHallAllAdapter;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.sgb.kjwl.model.entity.pick.PickInfoListEntity;
import com.sgb.kjwl.model.entity.pick.PreNoticeItemEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.event.CategoryEvent2;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickNoticeBidDetailActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickNoticeDetailActivity;
import com.sgb.kjwl.view.ui.adapter.pick.HomePickHallBidingAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.HomePickNoticeListAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.PickBidingAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.PickHallBidingAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.PickInfoListAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.PickWinningNoticeAdapter;
import com.sgb.kjwl.viewmodel.home.BiddingSignUpModel;
import com.sgb.zhaocai.entity.LabourWorkListEntity;
import com.sgb.zhaocai.entity.LeaseNeedListEntity;
import com.sgb.zhaocai.entity.MaterialListEntity;
import com.sgb.zhaocai.entity.ProjectListEntity;
import com.sgb.zhaocai.entity.SubpackagePlanListEntity;
import com.sgb.zhaocai.view.demand.adapter.LabourWorkAdapter;
import com.sgb.zhaocai.view.demand.adapter.LeaseNeedAdapter;
import com.sgb.zhaocai.view.demand.adapter.MaterialsBuyAdapter;
import com.sgb.zhaocai.view.demand.adapter.ProjectAdapter;
import com.sgb.zhaocai.view.demand.adapter.SubpackagePlanAdapter;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;


/**
 * Description:
 * Author cfk
 * Time
 * 招标竞价中的报名fragment
 */
public class BiddingSignUpFragment extends BaseHeightViewPagerFragment {
    private FragmentBiddingSignUpBinding binding;
    private MainActivity activity;
    private int mItemType = -1; //0招标公告 1直接竞价 2招标大厅 3中标公告 4招标预告 5需求预告 6招标信息
    @Inject
    BiddingSignUpModel viewModel;

    private PickHallAllNewAdapter pickHallAllSignUpAdapter; //招标大厅和招标公告
    private List<PickSelectEntity> typeSignUpDatas = new ArrayList<>();
    private String typeSignUpNos = "0";

    private List<PickSelectEntity> typeCompetitionDatas = new ArrayList<>();
    private PickBidingAdapter pickingCompetitionAdapter;
    //类型
    private String typeCompetitionNo;

    private List<PickSelectEntity> typeBiddingInformationDatas = new ArrayList<>();


    private PickWinningNoticeAdapter winningTheBidAdapter;
     //tablayout角标
    private int mCurrentIndex;

    private List<DictEntity3> typeDemandForecastDatas = new ArrayList<>();
    //类型
    private String typeDemandForecastNo = "0";

    private List<PublishListBean> publishListBeanData = new ArrayList<>();

    private ProjectAdapter projectadapter; //需求预告--工程项目
    private MaterialsBuyAdapter materialsadapter; //需求预告--材料采购
    private LeaseNeedAdapter leaseneedadapter; //需求预告--租赁需求
    private LabourWorkAdapter labourworkadapter; //需求预告--劳务用工
    private SubpackagePlanAdapter subpackageplanadapter;//需求预告--分包计划

    private PickInfoListAdapter pickInfoListAdapter;
    private String typeNo = "全部";

    private AuctionHallAdapter auctionhalladapter; //拍卖大厅
    private String auctionHallNo = "";

    //招标预告分类类目
    private HomePickNoticeListAdapter pickNoticeListAdapter;
    private List<PickSelectEntity> typeNoticeDatas = new ArrayList<>();
    private String typeNoticeNos = "0";

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof MainActivity) {
            activity = (MainActivity) context;
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() == null) {
            return;
        }
        mItemType = getItemType();
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    public static BiddingSignUpFragment newInstance(int position, BannerEntity bannerEntity, boolean isRefresh) {
        BiddingSignUpFragment fragment = new BiddingSignUpFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM, bannerEntity);
        args.putInt(ARG_PARAM_POS, position);
        args.putBoolean(ARG_isRefresh, isRefresh);
        fragment.setArguments(args);
        return fragment;
    }
    public void IsRefresh(boolean isRefresh) {
        setIsRefresh(isRefresh);
        switchTabLayout();
    }
    /**
     * 通过传进来的数据，来判断使用那个显示模型和数据加载模型
     *
     * @return
     */
    private int getItemType() {
        int mItemType = -1;
        BannerEntity bannerEntity = (BannerEntity) getArguments().getSerializable(ARG_PARAM);
        switch (bannerEntity.getName()) {
            case "招标公告":
                mItemType = 0;
                break;
            case "直接竞价":
                mItemType = 1;
                break;
            case "招标大厅":
                mItemType = 2;
                break;
            case "中标公告":
                mItemType = 3;
                break;
            case "招标预告":
                mItemType = 4;
                break;
            case "需求预告":
                mItemType = 5;
                break;
            case "招标信息":
                mItemType = 6;
                break;
            case "拍卖大厅":
                mItemType = 7;
                break;
        }
        return mItemType;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_bidding_sign_up, container, false);
        return binding.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(getActivity())) {
            EventBus.getDefault().register(getActivity());
        }
    }

    @Override
    public void initView() {
        super.initView();
        if (getArguments() == null) {
            return;
        }
        mItemType = getItemType();
        if (mItemType == -1) {
            return;
        }
        binding.tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getMoreData();
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        /*binding.recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = getResources().getDimensionPixelSize(R.dimen.dp1);
            }
        });*/
        binding.tvGoModel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().postSticky(CategoryEvent2.make(EventConstant.EVENT_CATEGORY_CHOOSE2, 2));
            }
        });
        binding.tvGoModel1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().postSticky(CategoryEvent2.make(EventConstant.EVENT_CATEGORY_CHOOSE2, 2));
            }
        });
        switch (mItemType) {
            case 0: // 招标公告
                if (pickHallAllSignUpAdapter == null) {
                    pickHallAllSignUpAdapter = new PickHallAllNewAdapter(getActivity(), null);
                }
                binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
                binding.recyclerView.setAdapter(pickHallAllSignUpAdapter);
                pickHallAllSignUpAdapter.setOnItemClick(new AdapterOnItemClick<TenderAllListNewEntity>() {
                    @Override
                    public void onItemClick(TenderAllListNewEntity tenderAllListNewEntity, int position) {
                        if (tenderAllListNewEntity.getDataSources() == 1) { //1.经营帮 2.政府网站
                            if (tenderAllListNewEntity.getBusinessType() == 1) {
                                if (tenderAllListNewEntity.getState() == 8) {
                                    PickNoticeDetailActivity.start(getActivity(), tenderAllListNewEntity.getId() + "");
                                } else {
                                    TenderDetailsActivity3.start(getContext(), tenderAllListNewEntity.getId() + "", 0);
                                }
                            } else if (tenderAllListNewEntity.getBusinessType() == 2) {
                                viewModel.getUrlSetting(tenderAllListNewEntity.getId());
                            }
                        } else if (tenderAllListNewEntity.getDataSources() == 2) {
                            viewModel.getUrlSettingSj(tenderAllListNewEntity.getId());
                        }
                    }
                });
                pickHallAllSignUpAdapter.onIChildClick(new PickHallAllNewAdapter.IChildClick() {
                    @Override
                    public void onClick(TenderAllListNewEntity tenderAllListNewEntity) {
                        if (tenderAllListNewEntity.getDataSources() == 1) { //1.经营帮 2.政府网站
                            if (tenderAllListNewEntity.getBusinessType() == 1) {
                                if (tenderAllListNewEntity.getState() == 8) {
                                    PickNoticeDetailActivity.start(getActivity(), tenderAllListNewEntity.getId() + "");
                                } else {
                                    TenderDetailsActivity3.start(getContext(), tenderAllListNewEntity.getId() + "", 0);
                                }
                            } else if (tenderAllListNewEntity.getBusinessType() == 2) {
                                viewModel.getUrlSetting(tenderAllListNewEntity.getId());
                            }
                        } else if (tenderAllListNewEntity.getDataSources() == 2) {
                            viewModel.getUrlSettingSj(tenderAllListNewEntity.getId());
                        }

                    }
                });
                break;
            case 1: //直接竞价
                if (pickingCompetitionAdapter == null) {
                    pickingCompetitionAdapter = new PickBidingAdapter(getActivity(), null);
                }
                binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
                binding.recyclerView.setAdapter(pickingCompetitionAdapter);

                pickingCompetitionAdapter.setOnItemClick(new AdapterOnItemClick<BiddingEntity>() {
                    @Override
                    public void onItemClick(BiddingEntity tenderListEntity, int position) {
                        viewModel.getUrlSetting(tenderListEntity.getId());
                    }
                });
                pickingCompetitionAdapter.onIChildClick(new PickBidingAdapter.IChildClick() {
                    @Override
                    public void onClick(BiddingEntity tenderListEntity) {
                        viewModel.getUrlSetting(tenderListEntity.getId());
                    }
                });
                break;
            case 2: //招标大厅
                pickHallAllSignUpAdapter = new PickHallAllNewAdapter(getContext(), null);
                binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
                binding.recyclerView.setAdapter(pickHallAllSignUpAdapter);
                pickHallAllSignUpAdapter.onIChildClick(new PickHallAllNewAdapter.IChildClick() {
                    @Override
                    public void onClick(TenderAllListNewEntity tenderAllListNewEntity) {
                        if (tenderAllListNewEntity.getDataSources() == 1) { //1.经营帮 2.政府网站
                            if (tenderAllListNewEntity.getBusinessType() == 1) {
                                if (tenderAllListNewEntity.getState() == 8) {
                                    PickNoticeDetailActivity.start(getActivity(), tenderAllListNewEntity.getId() + "");
                                } else {
                                    TenderDetailsActivity3.start(getContext(), tenderAllListNewEntity.getId() + "", 0);
                                }
                            } else if (tenderAllListNewEntity.getBusinessType() == 2) {
                                viewModel.getUrlSetting(tenderAllListNewEntity.getId());
                            }
                        } else if (tenderAllListNewEntity.getDataSources() == 2) {
                            viewModel.getUrlSettingSj(tenderAllListNewEntity.getId());
                        }
                    }
                });
                pickHallAllSignUpAdapter.setOnItemClick(new AdapterOnItemClick<TenderAllListNewEntity>() {
                    @Override
                    public void onItemClick(TenderAllListNewEntity tenderAllListNewEntity, int position) {
                        if (tenderAllListNewEntity.getDataSources() == 1) { //1.经营帮 2.政府网站
                            if (tenderAllListNewEntity.getBusinessType() == 1) {
                                if (tenderAllListNewEntity.getState() == 8) {
                                    PickNoticeDetailActivity.start(getActivity(), tenderAllListNewEntity.getId() + "");
                                } else {
                                    TenderDetailsActivity3.start(getContext(), tenderAllListNewEntity.getId() + "", 0);
                                }
                            } else if (tenderAllListNewEntity.getBusinessType() == 2) {
                                viewModel.getUrlSetting(tenderAllListNewEntity.getId());
                            }
                        } else if (tenderAllListNewEntity.getDataSources() == 2) {
                            viewModel.getUrlSettingSj(tenderAllListNewEntity.getId());
                        }
                    }
                });
                break;
            case 3: //中标公告
                winningTheBidAdapter = new PickWinningNoticeAdapter(getActivity(), null);
                binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
                binding.recyclerView.setAdapter(winningTheBidAdapter);
                winningTheBidAdapter.setOnItemClick(new AdapterOnItemClick<PreNoticeItemEntity>() {
                    @Override
                    public void onItemClick(PreNoticeItemEntity goodsInfoEntity3, int position) {
                        //中标
                        PickNoticeBidDetailActivity.start(getActivity(), goodsInfoEntity3.getTenderNo() + "");
                    }
                });
                binding.lineMore.setVisibility(View.GONE);
                binding.tabLayout.setVisibility(View.GONE);
                break;
            case 4: //招标预告
                pickNoticeListAdapter = new HomePickNoticeListAdapter(getActivity(), null);
                binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
                binding.recyclerView.setAdapter(pickNoticeListAdapter);
                pickNoticeListAdapter.setOnItemClick(new AdapterOnItemClick<PreNoticeItemEntity>() {
                    @Override
                    public void onItemClick(PreNoticeItemEntity preNoticeItemEntity, int position) {
                        if (preNoticeItemEntity.getTenderState() == 8) {
                            PickNoticeDetailActivity.start(activity, preNoticeItemEntity.getTenderNo() + "");
                        } else {
                            TenderDetailsActivity3.start(getActivity(),preNoticeItemEntity.getTenderNo(),0);
                        }
                    }
                });
                pickNoticeListAdapter.onIChildClick(new HomePickNoticeListAdapter.IChildClick() {
                    @Override
                    public void onClick(PreNoticeItemEntity preNoticeItemEntity) {
                        if (preNoticeItemEntity.getTenderState() == 8) {
                            PickNoticeDetailActivity.start(activity, preNoticeItemEntity.getTenderNo() + "");
                        } else {
                            TenderDetailsActivity3.start(getActivity(),preNoticeItemEntity.getTenderNo(),0);
                        }
                    }
                });
                binding.lineMore.setVisibility(View.GONE);
                binding.tabLayout.setVisibility(View.GONE);
                break;
            case 5: //需求预告
                binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
                //工程招采的适配器
                if (projectadapter == null) {
                    projectadapter = new ProjectAdapter(getActivity(), null);
                }
                //材料的适配器
                if (materialsadapter == null) {
                    materialsadapter = new MaterialsBuyAdapter(getActivity(), null);
                }
                //租赁的适配器
                if (leaseneedadapter == null) {
                    leaseneedadapter = new LeaseNeedAdapter(getActivity(), null);
                }
                //劳务的适配器
                if (labourworkadapter == null) {
                    labourworkadapter = new LabourWorkAdapter(getActivity(), null);
                }
                //工程施工
                if (subpackageplanadapter == null) {
                    subpackageplanadapter = new SubpackagePlanAdapter(getActivity(), null);
                }
                projectadapter.setOnItemClick(new AdapterOnItemClick<ProjectListEntity>() {
                    @Override
                    public void onItemClick(ProjectListEntity entity, int position) {
                        String longitude =  SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID,"");
                        String latitude =  SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID,"");
                        viewModel.getH5Url(entity.getId(), longitude, latitude);
                    }
                });
                subpackageplanadapter.setOnItemClick(new AdapterOnItemClick<SubpackagePlanListEntity>() {
                    @Override
                    public void onItemClick(SubpackagePlanListEntity entity, int position) {
                        String longitude =  SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID,"");
                        String latitude =  SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID,"");
                        viewModel.getsubpackageplanH5Url(entity.getId(), longitude, latitude);
                    }
                });
                break;
            case 6: //招标信息
                binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
                if (pickInfoListAdapter == null) {
                    pickInfoListAdapter = new PickInfoListAdapter(getActivity(), null);
                }
                binding.recyclerView.setAdapter(pickInfoListAdapter);
                pickInfoListAdapter.setOnItemClick(new AdapterOnItemClick<PickInfoListEntity>() {
                    @Override
                    public void onItemClick(PickInfoListEntity tenderListEntity, int position) {
                        viewModel.getH5Url(tenderListEntity.getId());
                    }
                });
                break;
            case 7: //拍卖大厅
                if (auctionhalladapter == null) {
                    auctionhalladapter = new AuctionHallAdapter(getActivity(), R.layout.ayction_hall_items1, null);
                }
                binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
                binding.recyclerView.setAdapter(auctionhalladapter);
                auctionhalladapter.setOnItemClick(new AdapterOnItemClick<AuctionListEntity>() {
                    @Override
                    public void onItemClick(AuctionListEntity auctionListEntity, int position) {
                        viewModel.getWebView(auctionListEntity.getNo());
                    }
                });
                break;
        }
    }

    @Override
    protected void onLazyLoad() {
        switch (mItemType) {
            case 0://招标公告
            case 2://招标大厅
            case 1://直接竞价
            case 4://招标预告
                viewModel.tenderNewTypeList(mItemType,getIsRefresh());
                break;
            case 3://中标公告
                viewModel.getWinningTheBidData(false, getPosition(), mItemType,getIsRefresh());
                break;
           /* case 4://招标预告
                viewModel.getBiddingListData(false, "", mItemType,getIsRefresh());
                break;*/
            case 5://需求预告
                viewModel.getFilterTypeDemandForecast(mItemType,getIsRefresh());
                break;
            case 6://招标信息
                viewModel.getTypeBiddingInformationData(mItemType,getIsRefresh());
                break;
            case 7://拍卖大厅
                viewModel.getAuctionHallTabList();
                break;
        }
    }

    @Override
    public void onVisible() {
        super.onVisible();
        if (binding.tabLayout.getTabCount() == 0 && mItemType != 3) {
            switch (mItemType) {
                case 0://招标公告
                case 1://直接竞价
                case 2://招标大厅
                    viewModel.tenderNewTypeList(mItemType,getIsRefresh());
                    break;
                case 3://中标公告
                    break;
                case 4://招标预告
                    break;
                case 5://需求预告
                    viewModel.getFilterTypeDemandForecast(mItemType,getIsRefresh());
                    break;
                case 6: //招标信息
                    viewModel.getTypeBiddingInformationData(mItemType,getIsRefresh());
                    break;
                case 7://拍卖大厅
                    viewModel.getAuctionHallTabList();
                    break;
            }
        } else {
            switchTabLayout();
        }
    }

    public <T> void setTypeDatas(List<T> datas) {
        if (datas == null || datas.size() == 0) {
            binding.tabLayout.setVisibility(View.GONE);
            binding.emptyView.setVisibility(View.VISIBLE);
            return;
        }
        binding.tabLayout.setVisibility(View.VISIBLE);
        List<PickSelectEntity> pickSelectEntityList = new ArrayList<>();
        List<DictEntity3> dictEntity3List = new ArrayList<>();
        List<PublishListBean> publishListBeanList = new ArrayList<>();
        if (mItemType == 5) {
            dictEntity3List.addAll((List<DictEntity3>) datas);
        } else if (mItemType == 7){
            publishListBeanList.addAll((List<PublishListBean>)datas);
        }else {
            pickSelectEntityList.addAll((List<PickSelectEntity>) datas);
        }
        switch (mItemType) {
            case 0://招标公告
            case 2://招标大厅
                typeSignUpDatas.clear();
                typeSignUpDatas.addAll(pickSelectEntityList);
                for (int i = 0; i < typeSignUpDatas.size(); i++) {
                    binding.tabLayout.addTab(binding.tabLayout.newTab().setText(typeSignUpDatas.get(i).getProjectscale()));
                }
                break;
            case 1://直接竞价
                typeCompetitionDatas.clear();
                typeCompetitionDatas.addAll(pickSelectEntityList);
                for (int i = 0; i < typeCompetitionDatas.size(); i++) {
                    binding.tabLayout.addTab(binding.tabLayout.newTab().setText(typeCompetitionDatas.get(i).getProjectscale()));
                }
                break;
            case 3://中标公告
                break;
            case 4://招标预告
                typeNoticeDatas.clear();
                typeNoticeDatas.addAll(pickSelectEntityList);
                for (int i = 0; i < typeNoticeDatas.size(); i++) {
                    binding.tabLayout.addTab(binding.tabLayout.newTab().setText(typeNoticeDatas.get(i).getProjectscale()));
                }
                break;
            case 5://需求预告
                if (datas == null || datas.size() == 0) {
                    binding.emptyView.setVisibility(View.VISIBLE);
                    return;
                }
                typeDemandForecastDatas.clear();
                typeDemandForecastDatas.addAll(dictEntity3List);
                for (int i = 0; i < typeDemandForecastDatas.size(); i++) {
                    binding.tabLayout.addTab(binding.tabLayout.newTab().setText(typeDemandForecastDatas.get(i).getName()));
                }
                break;
            case 6://招标信息
                typeBiddingInformationDatas.clear();
                PickSelectEntity entity = new PickSelectEntity();
                entity.setNo("");
                entity.setProjectscale("全部");
                typeBiddingInformationDatas.add(0, entity);
                typeBiddingInformationDatas.addAll(pickSelectEntityList);
                for (int i = 0; i < typeBiddingInformationDatas.size(); i++) {
                    binding.tabLayout.addTab(binding.tabLayout.newTab().setText(typeBiddingInformationDatas.get(i).getProjectscale()));
                }
                break;
            case 7://拍卖大厅
                if (datas == null || datas.size() == 0) {
                    binding.emptyView.setVisibility(View.VISIBLE);
                    return;
                }
                publishListBeanData.clear();
                /*if (publishListBeanList.size() > 0) {
                    publishListBeanList.get(0).setName("全部");
                }*/
                publishListBeanData.addAll(publishListBeanList);
                for (int i = 0; i < publishListBeanData.size(); i++) {
                    binding.tabLayout.addTab(binding.tabLayout.newTab().setText(publishListBeanData.get(i).getName()));
                }
                break;
        }
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                    mCurrentIndex = tab.getPosition();
                    switchTabLayout();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        switchTabLayout();
    }

    private void switchTabLayout() {
        switch (mItemType) {
            case 0://招标公告
            case 2://招标大厅
                typeSignUpNos = typeSignUpDatas.get(mCurrentIndex).getNo();
                viewModel.findTenderingEsPage(typeSignUpNos, mItemType,false);
                break;
            case 1: //直接竞价
                typeCompetitionNo = typeCompetitionDatas.get(mCurrentIndex).getNo();
                pickingCompetitionAdapter.setType(typeCompetitionNo);
                viewModel.getCompetitionList(typeCompetitionNo, false, getPosition(), mItemType,getIsRefresh());
                break;
            case 3://中标公告
                viewModel.getWinningTheBidData(false, getPosition(), mItemType,getIsRefresh());
                break;
            case 4://招标预告
                typeNoticeNos = typeNoticeDatas.get(mCurrentIndex).getNo();
                viewModel.getBiddingListData(typeNoticeNos, false);
                break;
            case 5://需求预告
                demandForecastSubCategoryOptions(false,mCurrentIndex);
                break;
            case 6://招标信息
                LogUtil.v("goodsInfo", "类型=" + typeBiddingInformationDatas.get(mCurrentIndex).getProjectscale());
                typeNo = typeBiddingInformationDatas.get(mCurrentIndex).getProjectscale();
                viewModel.getBiddingInformationList(typeNo, getPosition(), false, mItemType);
                break;
            case 7://拍卖大厅
                auctionHallNo = publishListBeanData.get(mCurrentIndex).getNo();
                viewModel.AuctionHallList(auctionHallNo, false);
                break;
        }
    }

    /**
     * 数据加载
     */
    private void getMoreData() {
        switch (mItemType) {
            case 0://招标公告
            case 2://招标大厅
                viewModel.findTenderingEsPage(typeSignUpNos, mItemType,true);
                break;
            case 1://直接竞价
                viewModel.getCompetitionList(typeCompetitionNo, true, getPosition(), mItemType,getIsRefresh());
                break;
            case 3://中标公告
                viewModel.getWinningTheBidData(true, getPosition(), mItemType,getIsRefresh());
                break;
            case 4://招标预告
                viewModel.getBiddingListData(typeNoticeNos,true);
                break;
            case 5://需求预告
                demandForecastSubCategoryOptions(true,mCurrentIndex);
                break;
            case 6://招标信息
                viewModel.getBiddingInformationList(typeNo, getPosition(), true, mItemType);
                break;
            case 7://招标大厅
                viewModel.AuctionHallList(auctionHallNo, true);
                break;
        }
    }


    public <T> void updateData(BaseRecycleAdapter<T> adapter, List<T> list, boolean isLoadMore) {
        //判断是否加载更多
        if (isLoadMore) {
            //判断列表数据是否为空
            if (list != null && list.size() > 0) {
                //适配器设置数据
                adapter.addDatas(list);
                if (list.size() < 3) {
                    //当数据少于3个时，隐藏加载更多按钮
                    binding.lineMore.setVisibility(View.VISIBLE);
                    binding.tvMore.setVisibility(View.GONE);
                } else {
                    //当数据数量正常时，默认显示加载更多按钮
                    binding.lineMore.setVisibility(View.VISIBLE);
                }
            } else {
                //当数据返回为空时，隐藏加载更多按钮
                binding.lineMore.setVisibility(View.VISIBLE);
                binding.tvMore.setVisibility(View.GONE);
            }
        } else {
            //判断列表数据是否为空
            if (list != null && list.size() > 0) {
                //当有数据时，显示加载更多按钮，隐藏缺省界面
                binding.lineMore.setVisibility(View.VISIBLE);
                if (list.size() < 3) {
                    //当数据少于3个时，隐藏加载更多按钮
                    binding.tvMore.setVisibility(View.GONE);
                } else {
                    binding.tvMore.setVisibility(View.VISIBLE);
                }
                binding.emptyView.setVisibility(View.GONE);
            } else {
                //当有数据时，隐藏加载更多按钮，显示缺省界面
                binding.lineMore.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
            //适配器设置数据
            adapter.setDatas(list);
        }
    }

    public <T> void showList(List<T> list, boolean isLoadMore) {
        switch (mItemType) {
            case 0://招标公告
            case 2://招标大厅
                showSignUpList((List<TenderAllListNewEntity>) list, isLoadMore);
                break;
            case 1://直接竞价
                showCompetitionList((List<BiddingEntity>) list, isLoadMore);
                break;
            case 3://中标公告
                showWinningTheBidList((List<PreNoticeItemEntity>) list, isLoadMore);
                break;
            case 4://招标预告
                showTenderNoticeBidList((List<PreNoticeItemEntity>) list, isLoadMore);
                break;
            case 5://需求预告
                switch (mCurrentIndex) {
                    case 0:
                        showProjectBuyList((List<ProjectListEntity>) list, isLoadMore);
                        break;
                    case 1:
                        showPickMaterialClassList((List<MaterialListEntity>) list, isLoadMore);
                        break;
                    case 2:
                        showPickDevice2List((List<LeaseNeedListEntity>) list, isLoadMore);
                        break;
                    case 3:
                        showPickLabourList((List<LabourWorkListEntity>) list, isLoadMore);
                        break;
                    case 4:
                        showPickPackersList((List<SubpackagePlanListEntity>) list, isLoadMore);
                        break;
                }
                break;
            case 6://招标信息
                showBiddingInformationList((List<PickInfoListEntity>) list, isLoadMore);
                break;
            case 7://拍卖大厅
                showAuctionHallList((List<AuctionListEntity>) list, isLoadMore);
                break;
            default:
                break;
        }
    }
    public void demandForecastSubCategoryOptions(boolean isLoadMore,int mCurrentIndex){
        switch (mCurrentIndex) {
            case 0://工程招采
                binding.recyclerView.setAdapter(projectadapter);
                viewModel.getList1(isLoadMore, getPosition(), "", mCurrentIndex,getIsRefresh());
                break;
            case 1://材料
                binding.recyclerView.setAdapter(materialsadapter);
                viewModel.getList2(isLoadMore, getPosition(), "", mCurrentIndex,getIsRefresh());
                break;
            case 2://租赁
                binding.recyclerView.setAdapter(leaseneedadapter);
                viewModel.getList3(isLoadMore, getPosition(), "", mCurrentIndex,getIsRefresh());
                break;
            case 3://劳务
                binding.recyclerView.setAdapter(labourworkadapter);
                viewModel.getList4(isLoadMore, getPosition(), "", mCurrentIndex,getIsRefresh());
                break;
            case 4://工程施工
                binding.recyclerView.setAdapter(subpackageplanadapter);
                viewModel.getList5(isLoadMore, getPosition(), "", mCurrentIndex,getIsRefresh());
                break;
        }
    }

    //招标公告 招标大厅
    public void showSignUpList(List<TenderAllListNewEntity> list, boolean isLoadMore) {
        updateData(pickHallAllSignUpAdapter, list, isLoadMore);
    }

    //直接竞价
    public void showCompetitionList(List<BiddingEntity> list, boolean isLoadMore) {
        updateData(pickingCompetitionAdapter, list, isLoadMore);
    }

    public void getUrl(String url) {
        WebViewX5HostActivity.start(getActivity(), url);
    }

    public void getUrl(String url, String id) {
        WebViewX5HostActivity.start(getActivity(), url, id);
    }

    private void showWinningTheBidList(List<PreNoticeItemEntity> list, boolean isLoadMore) {
        updateData(winningTheBidAdapter, list, isLoadMore);
    }

    private void showTenderNoticeBidList(List<PreNoticeItemEntity> list, boolean isLoadMore) {
        updateData(pickNoticeListAdapter, list, isLoadMore);
    }

    private void showBiddingInformationList(List<PickInfoListEntity> list, boolean isLoadMore) {
        updateData(pickInfoListAdapter, list, isLoadMore);
    }

    private void showAuctionHallList(List<AuctionListEntity> list, boolean isLoadMore){
        updateData(auctionhalladapter, list, isLoadMore);
    }

    private void showProjectBuyList(List<ProjectListEntity> list, boolean isLoadMore) {
        updateData(projectadapter, list, isLoadMore);
    }

    private void showPickDevice2List(List<LeaseNeedListEntity> list, boolean isLoadMore) {
        updateData(leaseneedadapter, list, isLoadMore);
    }

    private void showPickLabourList(List<LabourWorkListEntity> list, boolean isLoadMore) {
        updateData(labourworkadapter, list, isLoadMore);
    }

    private void showPickPackersList(List<SubpackagePlanListEntity> list, boolean isLoadMore) {
        updateData(subpackageplanadapter, list, isLoadMore);
    }

    private void showPickMaterialClassList(List<MaterialListEntity> list, boolean isLoadMore) {
        updateData(materialsadapter, list, isLoadMore);
    }

    public void showUrl(String url) {
        WebViewX5HostActivity.start(getActivity(), url);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
